package com.sskj.wallet.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.widget.BottomDialog;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.BillAssetBean;
import com.sskj.wallet.presenter.AssetDetailPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/27
 */
@Route(path = RConfig.WALLET_ACTIVITY_RECORD_ASSET)
public class AssetDetailActivity extends BaseActivity<AssetDetailPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tv_coin_type)
    TextView tvCoinType;
    @BindView(R2.id.ll_choose_coin)
    LinearLayout llChooseCoin;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    @Autowired
    int walletType;
    @Autowired
    String coinType;

    private int page = 1;
    private int size = 10;

    private BaseAdapter<BillAssetBean> adapter;
    List<String> coins = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.wallet_activity_asset_detail;
    }

    @Override
    public AssetDetailPresenter getPresenter() {
        return new AssetDetailPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.wallet_assetDetailActivity1));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 2))
                .setPaintColor(Color.TRANSPARENT));
        adapter = new BaseAdapter<BillAssetBean>(R.layout.wallet_item_record_asset, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, BillAssetBean item) {
                holder.setText(R.id.tv_type, item.getMemo())
                        .setText(R.id.tv_amount, item.getPrice() + " " + item.getPname())
                        .setText(R.id.tv_date, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000));
            }
        };
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener((recyclerView, position, v) -> {
            if (adapter.getData().size() == 0) {
                return;
            }
            ARouter.getInstance().build(RConfig.WALLET_ACTIVITY_DETAILS)
                    .withString(Constans.BILL_ID, adapter.getData().get(position).getId())
                    .navigation();
        });
    }

    @Override
    public void initData() {
        mPresenter.getBillAssets(true, walletType, coinType.toLowerCase(), page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getBillAssets(false, walletType, coinType.toLowerCase(), page, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getBillAssets(false, walletType, coinType.toLowerCase(), page, size);
        });
    }

    public void getBillAssetOK(List<BillAssetBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }
}
